package com.ibiaoke.boss.controller;

import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.customer.service.CustomerService;
import com.ibiaoke.goods.entity.PtGoods;
import com.ibiaoke.goods.service.PtGoodService;
import com.ibiaoke.order.entity.PtOrder;
import com.ibiaoke.order.service.PtOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;


@Controller
@RequestMapping("/ptGoods")
public class PtGoodController extends BaseController{

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private PtGoodService ptGoodService;

    @Autowired
    private PtOrderService ptOrderService;

    /**
     * 添加拼团商品
     * ptGoodsId ：商品id
     * ptGoodsName：商品名字
     * ptType:拼团类型(2:两人团  4：四人团  10：十人团)
     * param:商品参数
     * ptPrice：拼团价格
     * price : 原价
     * spec：商品规格
     * status：商品状态（0下架 1上架）
     * image：主图
     * thumImage：缩略图
     * detail：商品详情
     * startTime：活动结束时间
     * endTime: 活动结束时间
     */
    @RequestMapping("/savePtGoods")
    @ResponseBody
    public Res savePtGoods(PtGoods ptGood) throws ParseException {
        Logger logger = LoggerFactory.getLogger(this.getClass().getName());
        //SysUser user = getLoginUser();
        int count = ptGoodService.savePtGoods(ptGood);
        return Res.create().addResponse("saveGoods",count);
    }

    /**
     * 拼团商品查询(或根据条件查询,传什么参数，就根据什么参数条件查询)
     * @param ptGoodsName
     * @return
     */
    @RequestMapping("/getPtGood")
    @ResponseBody
    public Res getPtGood(String ptGoodsName,Integer ptGoodsId,Integer status,Page page){
        List<PtGoods>  PtGoodList  = ptGoodService.getPtGood(ptGoodsName,ptGoodsId,status,page);
        return Res.create().addResponse("PtGoodList", PtGoodList).addResponse("page", page);
    }

    /**
     * 拼团根据id查询商品
     */
    @RequestMapping("/getPtGoodsByPtId")
    @ResponseBody
    public Res getPtGoodsByPtId(Integer ptGoodsId){
        PtGoods  ptGood = ptGoodService.getPtGoodsByPtId(ptGoodsId);
        return Res.create().addResponse("ptGood",ptGood);
    }

    /**
     * 编辑修改拼团商品
     *
     */
    @RequestMapping("/editPtGood")
    @ResponseBody
    public Res editPtGood(PtGoods ptGood){
        //SysUser user = getLoginUser();
        int count = ptGoodService.editPtGood(ptGood);
        return Res.create().addResponse("count",count);
    }

    /**
     * 通过拼团商品id，删除拼团商品
     * @param ptGoodsId
     */
    @RequestMapping("/updatePtGood")
    @ResponseBody
    public Res updatePtGood(String  ptGoodsId){
       //SysUser user = getLoginUser();
        int deleteResult = ptGoodService.updatePtGood(ptGoodsId);
        //logger.info("系统用户{}操作了修改商品删除状态的操作",user.getUserName());
        return Res.create().addResponse("deleteResult",deleteResult);
    }

    /**
     * 查询拼团订单
     */
    @RequestMapping("/getPtOrder")
    @ResponseBody
    public Res getPtOrder(PtOrder ptOrder,Page page){
        List<PtOrder> listOrder = ptOrderService.getPtOrder(ptOrder,page,"0");
        return Res.create().addResponse("listOrder",listOrder).addResponse("page",page);
    }

    /**
     * 查询订单明细
     */
    @RequestMapping("/getPtOrderDeail")
    @ResponseBody
    public Res getOrderDetail(String ptOrderCode){
        List<PtOrder> listOrder = ptOrderService.getPtOrderDeatil(ptOrderCode);
        PtGoods  ptGood = ptGoodService.getPtGoodsByPtId(listOrder.get(0).getGoodId());
        return Res.create().addResponse("ptGood",ptGood).addResponse("OrderDetail",listOrder);
    }

    /**
     * poi一键导出Excel表
     */
    @RequestMapping("/getPtOrderByExcel")
    @ResponseBody
    public Res getPtOrderByExcel(PtOrder ptOrder, Page page, HttpServletRequest request, HttpServletResponse response){
        List<PtOrder> listOrder = ptOrderService.getPtOrder(ptOrder,page,"1");
        ptOrderService.getPtOrderByExcel(listOrder);
        ptOrderService.downloadFile(response, true);
        return Res.create().addResponse("listOrder",listOrder);
    }
}